/* 917. 仅仅反转字母 */
/**
 * @param {string} s
 * @return {string}
 */
const isLetter = s => {
    return s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z'
}
var reverseOnlyLetters = function (s) {
    let len = s.length
    let result = ""
    let j = 0
    for (let i = 0; i < len; i++) {
        if (isLetter(s[i])) {
            while (len - j >= 1) {
                const reverStr = s[len - j - 1]
                if (isLetter(reverStr)) {
                    break
                } else {
                    j++
                }
            }
            result += s[len - j - 1]
            j++
        } else {
            result += s[i]
        }
    }

    return result
};

// 双指针

var reverseOnlyLetters = function (s) {
    const n = s.length;
    const arr = [...s];
    let left = 0, right = n - 1;
    while (true) {
        while (left < right && !(/^[a-zA-Z]+$/.test(s[left]))) { // 判断左边是否扫描到字母
            left++;
        }
        while (right > left && !(/^[a-zA-Z]+$/.test(s[right]))) { // 判断右边是否扫描到字母
            right--;
        }
        if (left >= right) {
            break;
        }
        swap(arr, left, right);
        left++;
        right--;
    }
    return arr.join('');
};

const swap = (arr, left, right) => {
    const temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
}


reverseOnlyLetters('Test1ng-Leet=code-Q!')